package com.hk.blog.repository.jpa;

import com.hk.blog.domain.RolePermission;
import com.hk.core.data.jpa.repository.LongIdJpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Set;

public interface RolePermissionRepository extends LongIdJpaRepository<RolePermission> {

    @Query(value = "SELECT t.permissionId FROM RolePermission t WHERE t.roleId = ?1")
    Set<Long> findPermissionIdsByRoleId(Long roleId);

    @Modifying
    @Query(value = "DELETE FROM RolePermission t WHERE t.roleId = ?1")
    void deleteByRoleId(Long roleId);

    @Query(value = "SELECT t.permissionId FROM RolePermission t WHERE t.roleId IN(?1)")
    Set<Long> findPermissionIdsByRoleIds(Set<Long> roleIds);

    @Query(value = "SELECT t.roleId FROM RolePermission t WHERE t.permissionId IN(?1)")
    Set<Long> findRoleIdsByPermissionIds(Set<Long> permissionIds);

    @Query(value = "SELECT t.roleId FROM RolePermission t WHERE t.permissionId = ?1")
    Set<Long> findRoleIdsByPermissionId(Long permissionId);

}
